home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2516 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  1.6 KB

  1. Path: colossus.holonet.net!russell
  2. From: russell@news.mdli.com (Russell Blackadar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Recursive Definition?
  5. Date: 18 Jan 1996 01:03:02 GMT
  6. Organization: HoloNet National Internet Access System: 510-704-1058/modem
  7. Message-ID: <4dk686$9d8@colossus.holonet.net>
  8. References: <4df39j$f80@news.vcd.hp.com> <4dish8$43c@is.bbsrc.ac.uk>
  9. NNTP-Posting-Host: jubal.mdli.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Robert Tuck (Robert.Tuck@bbsrc.ac.uk) wrote:
  13. : [...]
  14. : >      static const Switch On;
  15. : >      static const Switch Off;
  16. : [...]
  17. : >const Switch Switch::On = 1;
  18. : >const Switch Switch::Off = 0;
  19. : >
  20. : >What are the above two statements trying to initialize?
  21. : >It seems to me that they are sort of doing a recursive
  22. : >initialization.  Please help!!!!
  23.  
  24. : It looks to me like On and Off should be ints, not Switchs. The 
  25. : respective lines should read:
  26.  
  27. :       static const int On;
  28. :       static const int Off;
  29.  
  30. : and
  31.  
  32. : const int Switch::On = 1;
  33. : const int Switch::Off = 0;
  34.  
  35. : A member of a class cannot be an object of that same class.
  36.  
  37. Yes it can, if it's a *static* member.  Infinite recursion does not
  38. occur, since there's only one copy of each static per program.
  39. The static members are not physically inside Switch objects, but
  40. rather, merely inside the Switch:: scope.  I admit it looks a bit
  41. confusing at first.
  42.  
  43. It might be very useful to have Switch::On/Off be Switchs, e.g.
  44. a function like void foo(const Switch&) could be called with the
  45. ready-made constant Switch::On, without having to construct a
  46. Switch on the fly.
  47. --
  48. Russell Blackadar,   russell@mdli.com
  49.